package com.shenjialin.Map;

import com.shenjialin.Game.Bullet;
import com.shenjialin.util.BulletsPool;
import com.shenjialin.util.Constant;
import com.shenjialin.util.MyUtil;

import java.util.*;
import java.awt.*;
import java.util.List;

public class MapTile {
    // 普通墙 图片
    protected static Image tileImg;
    //老家图片
    protected static Image tileImg1;
    // 不可破坏墙图片
    protected static Image tileImg2;

    // 老家
    public static final int  TILE_TYPE0 = 0;
    // 普通墙
    public static final int  TILE_TYPE1 = 1;
    // 不可破坏墙
    public static final int  TILE_TYPE2 = 2;

    protected int tileType;

    public static int tileW = 70;
    public boolean visible = true;

    public String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    static {
        // 加载墙的图片
        tileImg = MyUtil.createImage("res/blk.jpg");
        tileImg1 = MyUtil.createImage("res/Home.png");
        tileImg2 = MyUtil.createImage("res/blk1.jpg");
    }
    // 图片资源的左上角
    public int x,y;

    public MapTile(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw(Graphics g) {
        if(!visible) {
            return;
        }
        if(tileType == TILE_TYPE0) {
            g.drawImage(tileImg1,x,y,null);
        }else if(tileType == TILE_TYPE1) {
            g.drawImage(tileImg,x,y,null);
        }else if(tileType == TILE_TYPE2) {
            g.drawImage(tileImg2,x,y,null);
        }
    }

    public static void setTileImg(Image tileImg) {
        MapTile.tileImg = tileImg;
    }

    public MapTile() {

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    /**
     * 地图块和子弹的碰撞
     * @param
     * @return
     */
    public boolean isCollide(List<Bullet> bullets) {
        if(!visible) {
            return false;
        }
        for (Bullet bullet : bullets) {
            int bulletX = bullet.getX();
            int bulletY = bullet.getY();
            int radius = tileW >> 1;
            boolean collide = false;
            if(bullet.getBulletType() == Bullet.BULLET_TYPE0) {
                collide = MyUtil.isCollide(x + radius, y + radius, radius, bulletX, bulletY);
                // 子弹的销毁 TODO
                if (collide) {
                    bullet.setVisible(false);
                }
                BulletsPool.theReturn(bullet);
            }
            if(bullet.getBulletType() == Bullet.BULLET_TYPE1) {
                collide = MyUtil.isCollide(x + radius , y + radius, radius + Bullet.RADIUS1, bulletX, bulletY);
            }
            if (collide) {
                return true;
            }
        }
        return false;
    }

    public boolean isHouse() {
        return Constant.HOME_NAME.equals(name);
    }

    public void setTileType(int tileType) {
        this.tileType = tileType;
    }

    public int getTileType() {
        return tileType;
    }
}
